package com.awesumgames.awesum;

import static java.lang.Math.sqrt;
import static java.lang.Math.sin;
import static java.lang.Math.cos;
import java.util.Locale;

import static com.awesumgames.awesum.AwesumGlobal.Awesum.degToRad;

public final class Vector2 {

	public float x, y;
	
	public Vector2() {
		x = y = 0f;
	}
	public Vector2(Vector2 v2) {
		x = v2.x;
		y = v2.y;
	}
	public Vector2(float x, float y) {
		this.x = x; this.y = y;
	}
	public Vector2(double x, double y) {
		this.x = (float)x; this.y = (float)y;
	}
	
	public String toString() {
		return String.format(Locale.US, "(%f, %f)", x, y);
	}
	
	public Vector2 copy() {
		return new Vector2(this);
	}
	
	public boolean equals(Vector2 v2) {
		return x == v2.x && y == v2.y;
	}
	public boolean equals(float nx, float ny) {
		return x == nx && y == ny;
	}

	public Vector2 set(Vector2 v2) {
		this.x = v2.x; this.y = v2.y;
		return this;
	}
	public Vector2 set(float x, float y) {
		this.x = x; this.y = y;
		return this;
	}

	public Vector2 add(Vector2 v2) {
		x += v2.x; y += v2.y;
		return this;
	}
	public Vector2 add(float s) {
		x += s; y += s;
		return this;
	}
	public Vector2 add(float ax, float ay) {
		x += ax; y += ay;
		return this;
	}
	public Vector2 subtract(Vector2 v2) {
		x -= v2.x; y -= v2.y;
		return this;
	}
	public Vector2 multiply(float s) { //Scalar multiply
		x *= s; y *= s;
		return this;
	}
	public Vector2 multiply(Vector2 v2) { //Scalar multiply individual components
		x *= v2.x; y *= v2.y;
		return this;
	}
	public Vector2 divide(float s) { //Scalar divide
		x /= s; y /= s;
		return this;
	}
	public Vector2 divide(Vector2 v2) { //Scalar divide individual components
		x /= v2.x; y /= v2.y;
		return this;
	}
	public Vector2 mod(float s) { //Scalar modulo
		x %= s; y %= s;
		return this;
	}
	public Vector2 negate() {
		x=-x;y=-y; //2x fast as multiply(-1)
		return this;
	}
	public float magnitude() {
		return (float)sqrt(magnitudeSq());
	}
	public float magnitudeSq() {
		return x*x + y*y;
	}
	
	public Vector2 normalize() {
		return divide(magnitude());
	}
	public float dot(Vector2 v2) {
		return x*v2.x + y*v2.y;
	}
	/*public Vector2 cross(Vector2 v2) {
		return new Vector2(y*v2.z - z*v2.y, z*v2.x - x*v2.z, x*v2.y - y*v2.x);
	}*/
	public float distFrom(Vector2 v2) {
		return (float)sqrt(distSqFrom(v2));
	}
	public float distSqFrom(Vector2 v2) {
		float dx = x-v2.x, dy = y-v2.y;
		return dx*dx + dy*dy;
	}
	
	
	/*public Vector2 rotateX(float a) {
		a *= degToRad;
		
		double ny = cos(a) * y + sin(a) * z;
		double nz = -sin(a) * y + cos(a) * z;
		
		y = (float)ny;
		z = (float)nz;
		return this;
	}

	public Vector2 rotateY(float a) {
		a *= degToRad;
		
		double nx = cos(a) * x - sin(a) * z;
		double nz = sin(a) * x + cos(a) * z;
		
		x = (float)nx;
		z = (float)nz;
		return this;
	}

	public Vector2 rotateZ(float a) {
		a *= degToRad;
		
		double nx = cos(a) * x + sin(a) * y;
		double ny = -sin(a) * x + cos(a) * y;
		
		x = (float)nx;
		y = (float)ny;
		return this;
	}

	public Vector2 rotate(Vector2 v2) { //This might encounter a gimbal lock problem
		if (v2.x != 0f) rotateX(v2.x);
		if (v2.y != 0f) rotateY(v2.y);
		if (v2.z != 0f) rotateZ(v2.z);
		return this;
	}*/
}
